{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>no surfacing</th>\n",
       "      <th>flippers</th>\n",
       "      <th>fish</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>yes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>yes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   no surfacing  flippers fish\n",
       "0             1         1  yes\n",
       "1             1         1  yes\n",
       "2             1         0   no\n",
       "3             0         1   no\n",
       "4             0         1   no"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "row_data = {'no surfacing':[1,1,1,0,0],\n",
    "                'flippers':[1,1,0,1,1],\n",
    "                'fish':['yes','yes','no','no','no']}\n",
    "df = pd.DataFrame(row_data)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def calEnt(df):\n",
    "    p = df.iloc[:,-1].value_counts(True) \n",
    "    return np.sum(-p*np.log2(p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "函数功能：\n",
    "    寻找最佳切分列\n",
    "'''\n",
    "\n",
    "def bestSplit(df):\n",
    "    baseEnt = calEnt(df)\n",
    "    bestCol = ''\n",
    "    bestGain = 0\n",
    "    infoGain = 0\n",
    "\n",
    "    # 循环 每一个 特征列：['no surfacing', 'flippers']\n",
    "    for col in df.columns[:-1]:\n",
    "\n",
    "        p_v = df[col].value_counts(True)\n",
    "        ent_v = []\n",
    "        # 当前 特征的 每一个取值：'no surfacing': [1, 0]\n",
    "        for col_value in p_v.index:\n",
    "            # 根据 当前特征值 筛选 行\n",
    "            ent_v.append(calEnt(df[df[col] == col_value]))\n",
    "\n",
    "        # 当前列的 熵\n",
    "        colEnt = (p_v * ent_v).sum()\n",
    "\n",
    "        infoGain = baseEnt - colEnt\n",
    "        # print(col, \"的 信息增益为：\", infoGain)\n",
    "        # 把 信息增益 最大的列，最为结果返回\n",
    "        if (infoGain > bestGain):\n",
    "            bestGain = infoGain\n",
    "            bestCol = col\n",
    "\n",
    "    return bestCol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 按列 取值，删除 其行\n",
    "def mySplit(df,colName,value):\n",
    "    return df[df[colName]==value].drop(colName,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a6dc62afd8b03c17538a9dfce2fcb18f62cec380cc7b77050462a64b7e4e4814"
  },
  "kernelspec": {
   "display_name": "Python 3.8.0 32-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
